package app.netant.book.mobixs;

import java.io.File;
import java.net.URLEncoder;

import app.client.file.downs.FileDown;
/**
 * 久久 - 302 + 乱码处理
 * @author JiaJ
 *
 */
public class MobixsDown extends Thread {
	
	private String isDown = "0";
	
	private String filePath;
	private String url;
	
	private FileDown down;
	
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String isDown() {
		return isDown;
	}
	
	public void setUrl(String url) {
		this.url = url;
	}

	public void setDown(String isDown) {
		this.isDown = isDown;
	}

	@Override
	public void run() {
		
		try {
			
			File file = new File(this.filePath);
			if(file.exists()){
				file.delete();
			}
			
			while(true){
				
				String urlName = url.substring(url.lastIndexOf("/")+1);
				urlName = urlName.replaceAll("・", "·");
				urlName = URLEncoder.encode(urlName, "utf-8");
				String urlPlus = url.substring(0,url.lastIndexOf("/"));
				
				this.downFile(filePath, urlPlus + "/"+ urlName);
				this.isDown = "1";
				break;
			}
			
		} catch (Exception e) {
			
			this.isDown = "2";
			
			File file = new File(this.filePath);
			if(file.exists()){
				file.delete();
			}
			
			e.printStackTrace();
			
		}
		
	}
	
	public void downFile(String filePath,String url) throws Exception{		
		this.down = new FileDown();
		down.downImage(url, filePath);		
	}
	
	/**
	 * 强制关闭
	 */
	public void stopThread(){
		if(this.down != null){
			this.down.stop();
		}
	}
	
	public static void main(String[] args) throws Exception {
		
		System.out.println("开始测试下载");
		MobixsDown downControl = new MobixsDown();
		
		String url = "http://d1.mobixs.cn:86/book/神极_手机小说网_www.mobixs.cn.rar";
		
		String urlName = url.substring(url.lastIndexOf("/")+1);
		urlName = urlName.replaceAll("・", "·");
		urlName = URLEncoder.encode(urlName, "utf-8");
		String urlPlus = url.substring(0,url.lastIndexOf("/"));
		
		//String name = "神极_手机小说网_www.mobixs.cn.rar";
		
		System.out.println(urlPlus + "/"+ urlName);
		
	
		downControl.downFile("D:/测试.rar", urlPlus + "/"+ urlName);
		System.out.println("结束测试下载");
	}
	
}
